package com.groupbuying.store.controller.forInternal;

import com.groupbuying.store.connector.FileFegin;
import com.groupbuying.store.controller.forExternal.ChainStoresCounterApiController;
import com.groupbuying.store.service.ChainStoresApiService;
import com.groupbuying.store.service.StoreCheckoutCounterService;
import com.groupbuying.store.service.StoreCounterApiService;
import com.groupbuying.store.utils.JSONUtils;
import com.groupbuying.store.utils.ParamUtils;
import com.groupbuying.store.utils.R;
import com.groupbuying.store.utils.ReturnUtils;
import com.groupbuying.store.utils.apiresult.ApiResult;
import feign.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * @anthor wenguoqiang
 * @date: 2019/01/30 10:27
 * @company 够样
 * @des
 **/
@Api("连锁门店收银台接口")
@RequestMapping("/counter")
@RestController
public class ChainStoresCounterController {

    private static Logger log = LoggerFactory.getLogger(ChainStoresCounterController.class);
    @Autowired
    private ChainStoresApiService chainStoresApiService;

    @Autowired
    private StoreCounterApiService storeCounterApiService;

    @Autowired
    private FileFegin fileFegin;

    /**
     * 获取收款台列表
     *
     * @param mapParams
     * @return
     */
    @ApiOperation(value = "获取收款台列表接口", notes = "收款台列表接口")
    @PostMapping("/getCheckoutCounterList")
    public R getCheckoutCounterList(@RequestBody Map<String,Object> mapParams) {
       return chainStoresApiService.getCheckoutCounterList(mapParams);
    }

    /**
     * 新增收款台
     *
     * @paran mapParams
     * @return
     */
    @ApiOperation(value = "新增收款台", notes = "新增收款台接口")
    @PostMapping("/addCheckoutCounter")
    public R addCheckoutCounter(@RequestBody Map<String,Object> mapParams) {
        return chainStoresApiService.saveStoreCheckoutCounter(mapParams);
    }

    /**
     * fegin调用
     * @return
     */
    @GetMapping("/getStoreIdAndRuleIdByCounterId")
    public R getStoreIdAndRuleIdByCounterId(@RequestParam Map<String, Object> params){
        return chainStoresApiService.getStoreIdAndRuleIdByCounterId(params);
    }

    @GetMapping("/getStoreShopByCounterId")
    public Map<String,String> getStoreShopByCounterId(@RequestParam Map<String, Object> params){
        return chainStoresApiService.getStoreShopByCounterId(params);
    }

    @GetMapping("/getWxacode")
    public void getWxacode(String fileGrouoKey, HttpServletResponse httpServletResponse) {

        List<String> fileUuids = fileFegin.getFileUuids(fileGrouoKey);
        String fileUuid = fileUuids.get(0);
        Response response = fileFegin.rpcDownLoadFile(fileUuid);
        InputStream inputStream = null;
        try {
            inputStream = response.body().asInputStream();
            httpServletResponse.setContentType("image/png");
            OutputStream os = httpServletResponse.getOutputStream();
            int count = 0;
            byte[] buffer = new byte[0x100000]; //1M 缓存区
            while ((count = inputStream.read(buffer)) != -1)
                os.write(buffer, 0, count);
            os.flush();
            os.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 检测收银台是否可用
     * */
    @PostMapping("/checkCounter")
    public ApiResult checkCounter(@RequestParam Map<String, Object> params){

       return storeCounterApiService.checkCounter(params);
    }

}
